<?php

class Exam_AjaxController extends Core_Controller_Action_Standard 
{
  public function loadcategoryAction()
  {
        $parent_id = $this->_getParam('parent_id');
        $categories = Exam_Model_DbTable_Categories::getSubCategory($parent_id);
        return $this->_helper->json($categories->toArray());
        $data = Zend_Json::encode($categories->toArray());
        $this->getResponse()->setBody($categories->toArray());
  }
  public function editAnswerAction(){
      $answer_id = $this->_getParam('answer_id');
      $content = $this->_getParam('content');
      $answer = Engine_Api::_()->getItem('exam_answer', $answer_id);
      $answer->content = $content;
      $answer->save();
      $this->view->status = true;
      $this->view->message = 'Succesful';
      $this->_helper->contextSwitch->initContext();
  }
  
  public function editAnswerCheckAction(){
      $answer_id = $this->_getParam('answer_id');
      $value = $this->_getParam('value');
      $answer = Engine_Api::_()->getItem('exam_answer', $answer_id);
      $answer->result = $value;
      $answer->save();

      //update question
      $question = $answer->getQuestion();
      if($value){
          $question->number_choice++;
      }else{
          $question->number_choice--;
      }
      $question->save();

      $this->view->status = true;
      $this->view->message = 'Succesful';
      $this->_helper->contextSwitch->initContext();
  }
}